const env = require('../env.js')
/* 公共request 方法 */
export const request = ({
  url,
  data,
  method = "post"
},isLogin = true) => {
  let server = env.apiUrl; 
  const header = {}
  data = data || {};
  if (isLogin) {
    let token = wx.getStorageSync("token") || data.token
    header['Authorization'] = 'bearer ' + token;
  }
  return new Promise( (resolve, reject)=> {
    wx.request({
      url: server + url,
      method: method,
      data: data,
      header: header,
      success(res) {
        if (res.data.code !== 0) {
          wx.showToast({
            title: res.data.msg || '请求出错',
            icon: 'none',
            duration: 2000,
            mask: true
          })
          reject(res.data)
        }
        resolve(res.data)
      },
      fail(err){
        wx.showToast({
          title: '请求出错',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        reject(err)
      },
      complete() {}
    })
  }).catch((res) => {})
}